package com.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.model.po.Coupon;
import com.springboot.service.CouponService;

@RefreshScope	//监听动态刷新
@RestController
@RequestMapping("/coupons")
public class CouponController {
	@Autowired
	CouponService couponService;
	
	@GetMapping("/{typeId}")
	public ResponseEntity<Coupon> getCoupon(@PathVariable Integer typeId) {
		//获取typeId对应的优惠券
		Coupon coupon = couponService.getCoupon(typeId);
		if(coupon == null) {
			Coupon coupon1 = new Coupon();
			coupon1.setCouponImg("NOT_FOUND");
			return  ResponseEntity.status(HttpStatus.NOT_FOUND).body(coupon1);
		}else {
			return ResponseEntity.ok(coupon);
		}
	}
	
	@GetMapping
	public ResponseEntity listCoupon(){
		List<Coupon> list = couponService.listCoupon();
		if(list == null || list.size() == 0) {
			return  ResponseEntity.status(HttpStatus.NOT_FOUND).body("The requested resource was not found.");
		}else {
			return ResponseEntity.ok(list);
		}
	}

}
